package cn.zt.service.impl;

import cn.zt.common.Result;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zt.domain.CreditCard;
import cn.zt.service.CreditCardService;
import cn.zt.mapper.CreditCardMapper;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* @author asus
* @description 针对表【credit_card】的数据库操作Service实现
* @createDate 2024-09-05 19:01:55
*/
@Service
public class CreditCardServiceImpl extends ServiceImpl<CreditCardMapper, CreditCard>
    implements CreditCardService{

    @Override
    public Result addCreditCard(CreditCard creditCard) {
        creditCard.setDueDate(new Date());
        creditCard.setName("无敌嘎猛信用卡");
        boolean save = this.save(creditCard);
        return save ? Result.success() : Result.error();
    }

    @Override
    public Result selectByUid(Integer id) {
        CreditCard one = this.getOne(Wrappers.<CreditCard>lambdaQuery().eq(CreditCard::getUid, id));
        return Result.success(one);
    }

    @Override
    public Result rollMoney(CreditCard creditCard) {
        CreditCard one = this.getOne(Wrappers.<CreditCard>lambdaQuery().eq(CreditCard::getUid, creditCard.getUid()));
        //第一次借还,进行还款日期的设置
        if(one.getUsemoney() == 0){
            if(one.getLin() - one.getUsemoney() >= creditCard.getNum()){
                one.setUsemoney(one.getUsemoney() + creditCard.getNum());
                Calendar calendar=Calendar.getInstance();//获取一个日历对象
                Date date=calendar.getTime();
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                System.out.println("当前日期："+simpleDateFormat.format(date));//将日期转换为字符串
//                calendar.add(Calendar.YEAR,2);  //把当前的年份增加2年
                calendar.add(Calendar.MONTH,+1);  //把当前的月份加1月
                one.setDueDate(calendar.getTime());
                one.setRollDate(new Date());
                one.setLatePaymentFee(0.0);
                this.updateById(one);
            }
        }else {
            if(one.getLin() - one.getUsemoney() >= creditCard.getNum()){
                one.setUsemoney(one.getUsemoney() + creditCard.getNum());
                this.updateById(one);
            }else{
                if(one.getLin() - one.getUsemoney() >= creditCard.getNum() - (5000 - one.getOverLimitFee())){
                    one.setOverLimitFee(one.getOverLimitFee() + (creditCard.getNum() - (one.getLin() - one.getUsemoney())));
                    one.setUsemoney(10000.0);
                    one.setOverRollDate(new Date());
                    this.updateById(one);
                }else{
                    return Result.warn("超出额度");
                }
            }
        }
        return Result.success("借款成功");
    }

    @Override
    public Result countover(Integer id) {
        CreditCard one = this.getOne(Wrappers.<CreditCard>lambdaQuery().eq(CreditCard::getUid, id));
        //计算天数
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = new Date();
        Date d2 = one.getOverRollDate();
        long diff = d1.getDay() - d2.getDay();//这样得到的差值是毫秒级别
        System.out.println(diff+"-----------------------------");
        one.setExcessmoney(one.getOverLimitFee() * 0.005 *diff + one.getOverLimitFee());
        one.setLatePaymentFee(one.getUsemoney() + one.getUsemoney() * 0.015);
        boolean b = this.updateById(one);
        return b?Result.success():Result.error();
    }

    @Override
    public Result dueMoney(CreditCard creditCard) {
        CreditCard one = this.getOne(Wrappers.<CreditCard>lambdaQuery().eq(CreditCard::getUid, creditCard.getUid()));
        one.setUsemoney(0.0);
        one.setOverLimitFee(0.0);
        one.setLatePaymentFee(0.0);
        one.setExcessmoney(0.0);
        one.setRollDate(null);
        one.setOverRollDate(null);
        one.setDueDate(null);
        boolean b = this.updateById(one);
        return b?Result.success():Result.error();
    }

    @Override
    public Result dueDate(int id) {
        CreditCard one = this.getOne(Wrappers.<CreditCard>lambdaQuery().eq(CreditCard::getUid, id));
        if(one.getUsemoney() != 0) {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
            if (fmt.format(one.getDueDate()) .equals(fmt.format(new Date()))) {
                return Result.success("今天是还款日");
            }
        }
        return null;
    }

    @Override
    public Result yuqi(int id) {
        CreditCard one = this.getOne(Wrappers.<CreditCard>lambdaQuery().eq(CreditCard::getUid, id));
        Date date = new Date();
        if(date.after(one.getDueDate())){
            one.setCredit(one.getCredit() - 20);
            boolean b = this.updateById(one);
            return Result.success();
        }
        return Result.error();
    }


}





